﻿<!DOCTYPE html>
<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" href="css/demo.css" type="text/css">
	<link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="js/jquery.ztree.core.js"></script>
<title>文件上传表单</title>

<!-- <script src='ListDirfile.js' type="text/javascript"></script> -->
 <!-- <script> -->
                    <!-- exports.initialize2 = function(req, res){ -->
   <!-- console.log('initializing2');  -->
   <!-- res.send("来自服务器的响应"); } -->
 <!-- </script> -->
 <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="JSON-js/json2.js"></script>
 <script>
 function printclik()
 {
	alert("123");
	window.print(); 
 }
 function GetUrl(url)
 {
 alert('1');
	  var xmlhttp;
         var str;
          if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }
          else
          {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                  str = xmlhttp.responseText;//得到服务器响应
					 alert(str);
					DirPa(str);
              }
         }
         function getFilename(file) {
             var get = file.lastIndexOf("/");
             return file.substring(get + 1);
         }
	
          xmlhttp.open("GET",'/www',true);
          xmlhttp.send();
}

                        //AJAX
function loadXMLDoc()
{
     var xmlhttp;
    var str;
     if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
     }
     else
     {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function()
     {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
             str = xmlhttp.responseText;//得到服务器响应
			 alert(str);
           // str='["1.js","app.js","app11.js","express_server - 副本 (2).js","express_server - 副本.js","express_server.js","favicon.ico","FindFile.js","formParser.js","hello.js","http.js","httpServer.js","index.html","index.js","index2.html","linshi/","mime.js","mine.js","node_modules/","public/","requestHandlers.js","router.js","server.js","upload/","upload.html","upload.js","www/"]';
			
			
			// json 排序
			
			strToJosn(str);
			
			//json处理方式
			DirPa(str);
			// alert(obj.BlockList.length);
			// for(var i = 0; i < obj.BlockList.length; i++) 
			// {
			// 	  filePath = obj.BlockList[i];
			// 	  var arr = filePath.split('/');
			// 	  var len = arr.length;
			// 	  return arr[len-1];
			// }
            //document.getElementById("myDiv").innerHTML=str;
         }
    }
	function strToJosn(str)
	{
		// str转换为json 对象
		var obj = JSON.parse(str);
		// 第一次 直接遍历出所有一级目录的的文件 并且创建对应的目录的
		var treejosn = "";
		var vfiles= obj.Msg;
		var j = 1;
		var bHas = false; // 记录是否包含有点
		var id= 0;
		var TreeJson ;
		var file =	{"name" : "123","file" : "","path":"","Date":""};
		var dir  =  {"name" : "123","children" :[],open:true};
		var root = {
		  "name" : 'root',	
		  "children" : []
		  }    
	  // alert(root);
		//root.children[0] =child;
		for(var i = 0;i < vfiles.length;i++)
		{
			var vname = vfiles[i].name;
			var arr = vname.split('/');
			var len = arr.length;
			// 
			var curNode = root;
			for(var j = 1; j< len; j++)
			{
				if(j == len-1)// 最后一个 那么就是文件名称 
				{
					file.name = arr[j];
					file.file = vfiles[i].url;
					file.Date = vfiles[i].Date;
					file.path = vfiles[i].name;
					curNode.children.push(file);
				}
				else
				{
					dir.name = arr[j];
					curNode.children.push(dir);
					curNode = curNode.children;
				}
			}
		}	
	}
	
    function getFilename(file) {
        var get = file.lastIndexOf("/");
        return file.substring(get + 1);
    }

     <!-- xmlhttp.open("GET","http://192.168.1.157:8081/file_query?name=.zip&path=bugzip",true); -->
     <!-- xmlhttp.send(); -->
   <!-- } -->
   function DirPa(str)
   {
		var obj = JSON.parse(str);
            var catalog = [];
            for (var i = 0;i < obj.length;i++){
                catalog[i] = obj[i];
            }
            catalog.sort(function(a,b){
                if(a.indexOf("/") == -1){
                    a = 1;
                }else {
                    a = 0;
                };
                if(b.indexOf("/") == -1){
                    b = 1;
                }else {
                    b = 0;
                };
                return a-b;
            })
            var add = '<ul id="catalog">';
            for (var i = 0 ;i < catalog.length;i++){
                var arr = catalog[i].split("/");
                if(arr.length == 2){
                    add += '<li><a id="' + arr[0] + '"onclick="GetUrl(' + arr[0] + ')" href="javascript:void(0);">' + arr[0] + '</a></li>';
                }else {
                    add += '<li>' +arr[0] + '</li>';
                };
            }
            add += '</ul>';
            $("body").append(add);
   }
</script>
	<SCRIPT type="text/javascript">
		<!--
		var setting = {	};

		var zNodes =[
			{ name:"父节点1 - 展开", open:true,
				children: [
					{ name:"父节点11 - 折叠",
						children: [
							{ name:"叶子节点111"},
							{ name:"叶子节点112"},
							{ name:"叶子节点113"},
							{ name:"叶子节点114"}
						]},
					{ name:"父节点12 - 折叠",
						children: [
							{ name:"叶子节点121"},
							{ name:"叶子节点122"},
							{ name:"叶子节点123"},
							{ name:"叶子节点124"}
						]},
					{ name:"父节点13 - 没有子节点", isParent:true}
				]},
			{ name:"父节点2 - 折叠",
				children: [
					{ name:"父节点21 - 展开", open:true,
						children: [
							{ name:"叶子节点211"},
							{ name:"叶子节点212"},
							{ name:"叶子节点213"},
							{ name:"叶子节点214"}
						]},
					{ name:"父节点22 - 折叠",
						children: [
							{ name:"叶子节点221"},
							{ name:"叶子节点222"},
							{ name:"叶子节点223"},
							{ name:"叶子节点224"}
						]},
					{ name:"父节点23 - 折叠",
						children: [
							{ name:"叶子节点231"},
							{ name:"叶子节点232"},
							{ name:"叶子节点233"},
							{ name:"叶子节点234"}
						]}
				]},
			{ name:"父节点3 - 没有子节点", isParent:true}

		];

		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		});
		//-->
	</SCRIPT>
</HEAD>
<body>
<h3>文件上传：</h3>
选择一个文件上传: <br />
<form action="/file_upload" method="post" enctype="multipart/form-data">
<input type="file" name="name" size="50" />
<br/> 
文件路径: <input type="文件路径" name="path"><br>
是否解压zip文件: <input type="是否解压zip" name="ziptype"><br/>
<br />
<input type="submit" value="上传文件" />
</form>
<form action="/file_query" method="get">
文件名称: <input type="文件名称" name="name">  <br/>
<br/> 
文件路径: <input type="文件路径" name="path"><br/>
<br/>
<input type="submit" value="查询"><br/>
</form>

<form action="/file_delete" method="get">
文件名称: <input type="文件名称" name="name">  <br>
<br> 
文件路径: <input type="文件路径" name="path"><br>
<br>
<input type="submit" value="删除"><br>

</form>

<form action="/file_download" method="get">
文件名称: <input type="文件名称" name="name">  <br>
<br> 
文件路径: <input type="文件路径" name="path"><br>
<br>
<input type="submit" value="下载"><br>

</form>

<button type="button" onclick="loadXMLDoc()">请求数据</button>
<button type="button" onclick="printclik()">打印</button>
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
</div>
</body>
</html>